//
//  OverviewView.m
//  iMoney
//
//  Created by Sean McGrail on 2/10/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "OverviewView.h"
#import "iMoneyAppDelegate.h"

#define PIE_TAG = 12
#define BREAKDOWN_TAG = 13

CGPathRef makeRec(CGRect rect, CGFloat cornerRadius)
{
	//
	// Create the boundary path
	//
	CGMutablePathRef path = CGPathCreateMutable();
	CGPathMoveToPoint(path, NULL,
					  rect.origin.x,
					  rect.origin.y + rect.size.height - cornerRadius);
	
	// Top left corner
	CGPathAddArcToPoint(path, NULL,
						rect.origin.x,
						rect.origin.y,
						rect.origin.x + rect.size.width,
						rect.origin.y,
						cornerRadius);
	
	// Top right corner
	CGPathAddArcToPoint(path, NULL,
						rect.origin.x + rect.size.width,
						rect.origin.y,
						rect.origin.x + rect.size.width,
						rect.origin.y + rect.size.height,
						cornerRadius);
	
	// Bottom right corner
	CGPathAddArcToPoint(path, NULL,
						rect.origin.x + rect.size.width,
						rect.origin.y + rect.size.height,
						rect.origin.x,
						rect.origin.y + rect.size.height,
						cornerRadius);
	
	// Bottom left corner
	CGPathAddArcToPoint(path, NULL,
						rect.origin.x,
						rect.origin.y + rect.size.height,
						rect.origin.x,
						rect.origin.y,
						cornerRadius);
	
	// Close the path at the rounded rect
	CGPathCloseSubpath(path);
	
	return path;
}


@implementation OverviewView


-(IBAction) segmentAction:(UISegmentedControl*) sender
{	
	switch (sender.selectedSegmentIndex) {
		case 1:
		{
			pie.hidden = NO;
			breakdown.hidden =YES;
			
			break;
		}
		case 0:
		{
			pie.hidden = YES;
			breakdown.hidden =NO;
			
			break;
		}
		default:
			break;
	}
	
}


- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
		
		
		expenseListType = [[[iMoneyAppDelegate getMoneyState] getTypeList] retain];
		
        // Initialization code
		self.backgroundColor= 
			[UIColor clearColor];
		
		UIImageView* bImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"overview.png"]];
		
		UISegmentedControl *viewSelection = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects: @"Breakdown",  @"Pie chart",nil]];
		viewSelection.frame = CGRectMake(70, 5, 150, 20);
		viewSelection.selectedSegmentIndex = 0;
		viewSelection.tag = 3;
		[viewSelection addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
		viewSelection.segmentedControlStyle = UISegmentedControlStyleBar;
		[viewSelection setTintColor:[UIColor darkGrayColor]];
		
		
		UILabel *amountLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 30, 150, 20)];
		amountLabel.backgroundColor = [UIColor clearColor];
		amountLabel.textColor = [UIColor whiteColor];
		amountLabel.font = [UIFont systemFontOfSize:12.0];
		amountLabel.text = @"Net Amount:";
		
		
		netAmount = [[UILabel alloc ] initWithFrame:CGRectMake(120, 50, 150, 25)];
		netAmount.textAlignment =  UITextAlignmentLeft;
		netAmount.textColor = [UIColor whiteColor];
		netAmount.backgroundColor = [UIColor clearColor];
		netAmount.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(18.0)];
		MoneyState* ms = [iMoneyAppDelegate getMoneyState];
		netAmount.text = [NSString stringWithFormat:@"%0.2f", [ms getTotalAccountsAmount]];
		
		breakdown = [[UITableView alloc] initWithFrame: CGRectMake(0, 100, self.bounds.size.width, self.bounds.size.height-100) 
												 style: UITableViewStyleGrouped];
		breakdown.backgroundColor = [UIColor clearColor];
		[breakdown setDataSource:self];
		[breakdown setDelegate:self];
		[self addSubview:breakdown];
		
		
		pie = [[PieChartView alloc] initWithFrame:CGRectMake(0,(self.frame.size.height/5) +10, 
															 self.frame.size.width, self.frame.size.height-90)];
		pie.tag = 12;
		pie.hidden = YES;
		pie.backgroundColor =[UIColor clearColor];
		[self addSubview:pie];
		[pie release];
		
		[self addSubview:bImage];
		[self sendSubviewToBack:bImage];
		[bImage release];
		
		[self addSubview:viewSelection];
		[viewSelection release];
		
		[self addSubview:amountLabel];
		[amountLabel release];
		
		[self addSubview:netAmount];
		
		
	
		
		
    }
    return self;
}






- (void)drawRect:(CGRect)rect {
    
}


//----------------- table actions


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	
	NSInteger c = [expenseListType count];
	
	
    return c;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"ExpenseCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
	//diable highlighting
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	cell.accessoryType = UITableViewCellAccessoryNone;
	
	[cell setBackgroundColor:[UIColor colorWithRed:211.0/255.0 green:211.0/255.0 blue:211.0/255.0 alpha:1.0]];
	
	cell.textLabel.textColor = [UIColor blackColor];
	cell.textLabel.text = [expenseListType objectAtIndex:indexPath.row];
	
	
	
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	//DO nothing
}



- (void)dealloc {
	
	[expenseListType release];
	[breakdown release];
	[pie release];
    [super dealloc];
}


@end
